package org.apache.dolphinscheduler.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apache.dolphinscheduler.api.enums.Status;
import org.apache.dolphinscheduler.api.service.ApplyService;
import org.apache.dolphinscheduler.api.utils.PageInfo;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.dao.entity.Apply;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.mapper.ApplyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApplyServiceImpl extends BaseServiceImpl implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    @Override
    public Result add(User loginUser, Apply apply) {
        Result result = new Result();
        apply.setApplyType(1);
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        apply.setApplyTime(now.format(formatter).toString());
        int insert = applyMapper.insert(apply);
        if(insert > 0){
            result.setData(apply);
        }
            putMsg(result, Status.SUCCESS);
        return result;
    }

    @Override
    public Result audit(User loginUser, int id, int type, String account, String remark) {
        Result result = new Result();
        Apply apply = applyMapper.selectById(id);
        if (apply == null) {
            putMsg(result, Status.APPLY_NOT_EXIST);
            return result;
        }
        apply.setApplyType(type);
        System.out.println(type);
        if(account != null && !account.isEmpty()){
            apply.setApplyAccount(account);
        }
        if(remark != null && !remark.isEmpty()){
            apply.setApplyComment(remark);
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        apply.setAuditTime(now.format(formatter).toString());
        int update = applyMapper.updateById(apply);
        if (update > 0) {
            putMsg(result, Status.SUCCESS);
        }
        return result;
    }

    @Override
    public Result list(User loginUser, Integer pageSize, Integer pageNo, String type, String searchVal,String applyAccount) {
        Result result = new Result();

        PageInfo<Apply> pageInfo = new PageInfo<>(pageNo, pageSize);
        Page<Apply> page = new Page<>(pageNo, pageSize);
        IPage<Apply> applyIPage = null;
        LambdaQueryWrapper<Apply> lqw = new LambdaQueryWrapper<Apply>();
        lqw.like(StringUtils.isNotBlank(type),Apply::getApplySupercomputing, type);
        lqw.like(StringUtils.isNotBlank(searchVal),Apply::getApplyName, searchVal);
        lqw.like(loginUser.getUserType().equals(1),Apply::getApplyAccount, loginUser.getUserName());
        lqw.like(StringUtils.isNotBlank(applyAccount),Apply::getApplyAccount, applyAccount);

        applyIPage = applyMapper.selectPage(page, lqw);
        List<Apply> applyList = applyIPage.getRecords();

        pageInfo.setTotal((int) applyIPage.getTotal());
        pageInfo.setTotalList(applyList);
        result.setData(pageInfo);
        putMsg(result, Status.SUCCESS);
        return result;
    }

    @Override
    public Result saveToken(User loginUser, Apply apply) {
        Result result = new Result();
        Map map = new HashMap();
        map.put("applyAccount",apply.getApplyAccount());
        if(apply.getApplyToken() != null && !apply.getApplyToken().isEmpty()){
            map.put("applyToken", apply.getApplyToken());
        }
        if(apply.getApplyPassword() != null && !apply.getApplyPassword().isEmpty()){
            map.put("applyPassword", apply.getApplyPassword());
        }
        int update = applyMapper.updateTokenOrPwd(map);
        if (update > 0) {
            putMsg(result, Status.SUCCESS);
        } else {
            putMsg(result, Status.ERROR);
        }
        return result;
    }

    @Override
    public Result get(User loginUser, int id) {
        Result result = new Result();
        Apply apply = applyMapper.selectById(id);
        if (apply != null) {
            result.setData(apply);
            putMsg(result, Status.SUCCESS);
        } else {
            List<Apply> applyList = applyMapper.selectList(null);
            result.setData(applyList);
            putMsg(result, Status.SUCCESS);
        }
        return result;
    }
}
